//
//  MZReadingViewController.m
//  WHISPER
//
//  Created by lanou3g on 15/12/17.
//  Copyright © 2015年 王艳苹. All rights reserved.
//

#import "MZReadingViewController.h"
#import "MZReadingView.h"
#import "MZReadingViewCell.h"
#import "YPMakeURL.h"
#import "YPNetWorking.h"
#import "UIImageView+WebCache.h"
#import "MZReadingModel.h"
#import "MZReadingDetailController.h"
#import "SDCycleScrollView.h"
#import "YPWebViewVC.h"
@interface MZReadingViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, SDCycleScrollViewDelegate>
@property (nonatomic, strong)MZReadingView *rv;
@property (nonatomic, strong)NSMutableArray *imgArr;
@property (nonatomic, strong)NSMutableArray *collectionArr;
@end

@implementation MZReadingViewController

- (void)loadView
{
    self.rv = [[MZReadingView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = self.rv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rv.cv.delegate = self;
    self.rv.cv.dataSource = self;
    self.rv.cv.backgroundColor = [UIColor whiteColor];
    self.imgArr = [NSMutableArray array];
    self.collectionArr = [NSMutableArray array];
    [[YPNetWorking shareNetWorking] getDictWithURL:[YPMakeURL shareMakeURL].readURL dict:[YPMakeURL shareMakeURL].readWheelDic success:^(NSDictionary *dict) {
        for (NSDictionary *dic in dict[@"data"][@"carousel"]) {
            MZReadingModel *m = [[MZReadingModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.imgArr addObject:m];
        }
        for (NSDictionary *d in dict[@"data"][@"list"]) {
            MZReadingModel *m = [[MZReadingModel alloc] init];
            [m setValuesForKeysWithDictionary:d];
            [self.collectionArr addObject:m];
        }
        [self.rv.cv reloadData];
        [self setupView];
    } fail:^(NSError *err) {
        
    }];
    [self.rv.cv registerClass:[MZReadingViewCell class] forCellWithReuseIdentifier:@"ReuseId"];
    
}

- (void)setupView
{
    NSMutableArray *scArr = [NSMutableArray array];
    for (int i = 0; i < self.imgArr.count; i++) {
        MZReadingModel * m = self.imgArr[i];
        [scArr addObject:m.img];
    }
    
    SDCycleScrollView *sd = [SDCycleScrollView cycleScrollViewWithFrame:self.rv.sv.frame imageURLStringsGroup:scArr];
    [self.rv.sv addSubview:sd];
    sd.delegate = self;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"ReuseId";
    MZReadingViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];
    if (!cell) {
        cell = [[MZReadingViewCell alloc] initWithFrame:CGRectMake(0, 0, 120 * ([UIScreen mainScreen].bounds.size.width / 375.0), 120 * ([UIScreen mainScreen].bounds.size.height / 667.0))];
    }
    MZReadingModel *m = self.collectionArr[indexPath.item];
    [cell.iv sd_setImageWithURL:[NSURL URLWithString:m.coverimg]];
    cell.nineLabel.text = [NSString stringWithFormat:@" %@ %@",m.name,m.enname];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MZReadingDetailController *rdVC = [[MZReadingDetailController alloc] init];
    MZReadingModel *m = self.collectionArr[indexPath.row];
    rdVC.typeID = m.type;
    rdVC.typeName = m.name;
    [self.navigationController presentViewController:[[UINavigationController alloc] initWithRootViewController:rdVC] animated:YES completion:nil];
    
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    YPWebViewVC *webVC = [[YPWebViewVC alloc] init];
    MZReadingModel *m = self.imgArr[index];
    NSString *str = [NSString stringWithFormat:@"http://pianke.me/webview/%@?f=appshare",[m.url substringFromIndex:17]];
    webVC.tingID = str;
    [self.navigationController presentViewController:[[UINavigationController alloc] initWithRootViewController:webVC] animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
